import {onBeforeUnmount, onMounted, ref} from "vue";

export default function () {
    // 初始化坐标数据
    const x = ref(-1)
    const y = ref(-1)
    //点击事件的回调函数
    const clickHandler = (event:MouseEvent) =>{
        x.value = event.pageX
        y.value = event.pageY
    }

    // 挂载后绑定点击事件
    onMounted(() => {
        window.addEventListener('click',clickHandler)
    })
    // 页面卸载之前的生命周期组合API
    onBeforeUnmount(() => {
        window.removeEventListener('click',clickHandler)
    })
    return {
        x,
        y
    }
}
